package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.database.DataSetObservable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.SystemMessBean;
import com.itboye.ihomebank.ttKey.Key;

import java.util.List;

/**
 * Created by Itboye-000018 on 2017/7/20.
 */
public class KeyThreeAdapter extends BaseecyclerKeyAdapter<KeyThreeAdapter.ViewHolder>{
    List<Key> list;
    private int mLastPosition = -1;
    public KeyThreeAdapter(Context context) {
        super(context);
    }

    @Override
    public void notifyDataSetChanged(List<Key> list) {
        this.list = list;
        super.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= getInflater().inflate(R.layout.item_my_key,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (list.get(position).getTypes().equals("0")) {
            holder.my_key_type.setText("管理员");
        } else{
            if (list.get(position).getLock_type()!=null&&list.get(position).getLock_type().equals("6587")){//白马锁
                if (list.get(position).getTypes().equals("0")){
                    holder.my_key_type.setText("管理员");
                }else  if (list.get(position).getTypes().equals("1")){
                    holder.my_key_type.setText("普通用户");
                }else  if (list.get(position).getTypes().equals("2")){
                    holder.my_key_type.setText("租户管理员");
                }else{
                    holder.my_key_type.setText("普通用户");
                }
            }else{//科技侠
                if (list.get(position).getUser_type()!=null){
                    if(list.get(position).getUser_type().equals("1")){
                        holder.my_key_type.setText("授权用户");
                    }else{
                        holder.my_key_type.setText("普通用户");
                    }
                }else{
                    holder.my_key_type.setText("普通用户");
                }
            }
        }
        if (list.get(position).getStartDate()==0&&list.get(position).getEndDate()==0){
            holder.my_key_xianshi.setVisibility(View.GONE);
        }else{
            holder.my_key_xianshi.setVisibility(View.VISIBLE);
        }
        holder.item_my_zhuangtai.setText(list.get(position).getKeyStatus());
        holder.my_key_name.setText(list.get(position).getLockAlias());
        holder.item_my_dianliang.setText(list.get(position).getBattery()<=0? "" : "剩余电量:" + list.get(position).getBattery()+ "%");
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView my_key_type,item_my_zhuangtai,my_key_name,item_my_dianliang,my_key_xianshi;
        public ViewHolder(View itemView) {
            super(itemView);
            my_key_type = (TextView) itemView.findViewById(R.id.my_key_type);
            item_my_zhuangtai = (TextView) itemView.findViewById(R.id.item_my_zhuangtai);
            my_key_name = (TextView) itemView.findViewById(R.id.my_key_name);
            item_my_dianliang = (TextView) itemView.findViewById(R.id.item_my_dianliang);
            my_key_xianshi = (TextView) itemView.findViewById(R.id.my_key_xianshi);
        }
    }
    private final DataSetObservable mDataSetObservable = new DataSetObservable();
    /**
     * 通知所附观察员，基础数据已更改，任何反映数据集的视图都应该刷新自己。
     */
    public void notifyListDataSetChanged() {
        mDataSetObservable.notifyChanged();
    }
    public KeyThreeAdapter refresh(List<Key> list) {
        list.clear();
        list.addAll(list);
        notifyDataSetChanged();
        notifyListDataSetChanged();
        mLastPosition = -1;
        return this;
    }
    public KeyThreeAdapter loadmore(List<Key> list) {
        list.addAll(list);
        notifyDataSetChanged();
        notifyListDataSetChanged();
        return this;
    }
    //添加一条数据
    public void addData(int position) {
        // mDataList.add(position, "Insert One");
        notifyItemInserted(position);
    }

    //删除一条数据
    public void removeData(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

}
